<?php
/*
 * program:	均源
 * author:	雨中漫步 & 上善若水 & qhxin
 * email:	qhxin@qhxin.com
 * date:	2014/11/11
 * description:	又是一年双十一，不能剁手，就写点东西吧~~~~~~
 */

defined('IN_SCHOOL') or die();

/*{TranslateTagBegin}*/
class control_app extends base_control{

	private $original_token;
	//hook ctr_app_before.php

	public function __construct() {
	
		//hook ctr_app_parent_init_before.php
		parent::__construct();
		
		$this->original_token = CONFIG_CS_KEY.substr($_SERVER['time'], 0, -2);
		define('APP_OUT_JSON',1);
		//hook ctr_app_parent_init_after.php
	}
	
	public function do_index() {
		$this->app_valid();
		//hook ctr_app_index_start.php
		$this->app_out('Hello JYWAP App');
	}
	
	/*
	获取一段时间($time秒)内发布的热门回复帖子$limit个
	index.php?app-hottalklist-time-3600000-limit-10.htm
	*/
	public function do_hottalklist() {
		$this->app_valid();
		$time = core::gpc('time', 'G');
		$time = empty($time) ? 3600 : intval($time);
		$limit = core::gpc('limit', 'G');
		$limit = empty($limit) ? 10 : intval($limit);
		//hook ctr_app_do_hottalklist.php
		
		$datetime = date('Y-m-d H:i:s', $_SERVER['time'] - $time);
		//hook ctr_app_do_hottalklist_list_before.php
		$re = $this->db->index_fetch('talks', 'tid'
				, array('posttime'=>array('>=' => $datetime),'fid'=>array('>' => 0))
				, array('tj'=> -1)
				, 0, $limit);
		$res = array();
		//hook ctr_app_do_hottalklist_list_after.php
		foreach($re as &$v) {
			unset($v['content']);
			$res[] = $v;
		}
		$this->app_out($res);
	}
	
	/*
	获取精品帖子$limit个
	index.php?app-digesttalklist-limit-10.htm
	*/
	public function do_digesttalklist() {
		$this->app_valid();
		//hook ctr_app_do_digesttalklist.php
		
		$limit = core::gpc('limit', 'G');
		$limit = empty($limit) ? 10 : intval($limit);
		
		//hook ctr_app_do_digesttalklist_list_before.php
		$re = $this->db->index_fetch('talks', 'tid'
				, array('isdigest'=> 1, 'fid'=>array('>' => 0) ), array('tid'=> -1)
				, 0, $limit);
		$res = array();
		//hook ctr_app_do_digesttalklist_list_after.php
		foreach($re as &$v) {
			unset($v['content']);
			$res[] = $v;
		}
		$this->app_out($res);
	}
	
	/*
	获取$fid板块对应的新贴$limit个
	index.php?app-forumtalklist-fid-1-limit-10.htm
	*/
	public function do_forumtalklist() {
		$this->app_valid();
		$res = array();
		//hook ctr_app_do_forumtalklist.php
		$fid = intval(core::gpc('fid', 'G'));
		if(empty($fid) || $fid == 0) $this->app_out($res);
		
		$limit = core::gpc('limit', 'G');
		$limit = empty($limit) ? 10 : intval($limit);
		
		//hook ctr_app_do_forumtalklist_list_before.php
		$re = $this->db->index_fetch('talks', 'tid'
				, array('fid'=> $fid), array('tid'=> -1)
				, 0, $limit);
		//hook ctr_app_do_forumtalklist_list_after.php
		foreach($re as &$v) {
			unset($v['content']);
			$res[] = $v;
		}
		$this->app_out($res);
	}
	
	/*
	获取$tid帖子内容
	index.php?app-tidtalkshow-tid-36.htm
	*/
	public function do_tidtalkshow() {
		$this->app_valid();
		$res = array();
		//hook ctr_app_do_tidtalkshow.php
		$tid = intval(core::gpc('tid', 'G'));
		if(empty($tid)) $this->app_out($res);
		
		//hook ctr_app_do_tidtalkshow_before.php
		$res = $this->db->get('talks-tid-'.$tid);
		
		if(empty($res) || $res['fid'] == 0) $res = array();
		//hook ctr_app_do_tidtalkshow_after.php
		
		$this->app_out($res);
	}
	
	/*
	获取$tid帖子回复列表
	index.php?app-tidcommlist-tid-36-order-1-start-0-limit-10.htm
	*/
	public function do_tidcommlist() {
		$this->app_valid();
		$res = array();
		//hook ctr_app_do_tidcommlist.php
		$tid = intval(core::gpc('tid', 'G'));
		if(empty($tid)) $this->app_out($res);
		
		$order = core::gpc('order', 'G');
		$order = (empty($order) || $order==1) ? -1 : 1;
		
		$start = core::gpc('start', 'G');
		$start = empty($start) ? 0 : intval($start);
		
		$limit = core::gpc('limit', 'G');
		$limit = empty($limit) ? 10 : intval($limit);
		
		//hook ctr_app_do_tidcommlist_list_before.php
		$res = $this->db->index_fetch('comm', 'cid'
				, array('fid'=> $tid), array('cid'=> $order)
				, $start, $limit);
		//hook ctr_app_do_tidcommlist_list_after.php
		$this->app_out($res);
	}
	
	/*
	获取$cid回复层聊列表
	index.php?app-cidsublist-cid-36_35-order-1-start-0-limit-10.htm
	*/
	public function do_cidsublist() {
		$this->app_valid();
		$res = array();
		//hook ctr_app_do_cidsublist.php
		$cid = core::gpc('cid', 'G');
		if(empty($cid)) $this->app_out($res);
		
		$cids = explode('_', $cid);
		$cid = array();
		foreach($cids as $k=>$v) {
			$v = intval($v);
			if($v>0) $cid[] = $v;
		}
		$cid = array_unique($cid);
		$order = core::gpc('order', 'G');
		$order = (empty($order) || $order==1) ? -1 : 1;
		
		$start = core::gpc('start', 'G');
		$start = empty($start) ? 0 : intval($start);
		
		$limit = core::gpc('limit', 'G');
		$limit = empty($limit) ? 10 : intval($limit);
		
		//hook ctr_app_do_cidsublist_list_before.php
		foreach($cid as $k=>$v) {
			$re = $this->db->index_fetch('subrpl', 'rid'
				, array('fid'=> $v), array('rid'=> $order)
				, $start, $limit);
			foreach($re as $k=>&$ea){
				//hook ctr_app_do_cidsublist_parse_rpl.php
				$this->parse->parse_face($ea['content']);
				$res["comm-cid-$v"][] = $ea;
			}
		}
		//hook ctr_app_do_cidsublist_list_after.php
		$this->app_out($res);
	}
	
	/*
	获取$uid用户信息
	index.php?app-uiduser-uid-1_2.htm
	*/
	public function do_uiduser() {
		$this->app_valid();
		$res = array();
		//hook ctr_app_do_uiduser.php
		$uid = core::gpc('uid', 'G');
		if(empty($uid)) $this->app_out($res);
		
		$uids = explode('_', $uid);
		$uid = array();
		foreach($uids as $k=>$v) {
			$v = intval($v);
			if($v>0) $uid[] = $v;
		}
		$uid = array_unique($uid);
		//hook ctr_app_do_uiduser_list_before.php
		foreach($uid as $k=>$v) {
			$res["user-uid-$v"] = $this->user->get_user_by_uid($v, false);
			if($res["user-uid-$v"] == false) {
				$res["user-uid-$v"] = array();
			}
			if(!empty($res["user-uid-$v"]))
				unset($res["user-uid-$v"]['ssid']);
			$res["user-uid-$v"] = $this->user->user_format($res["user-uid-$v"]);
		}
		//hook ctr_app_do_uiduser_list_after.php
		$this->app_out($res);
	}
	
	/*
	获取token
	index.php?app-token-key-{md5(cskey)}.htm
	*/
	public function do_token() {
		//hook ctr_app_do_token.php
		$app_key = core::gpc('key', 'R');
		$original_key = md5(CONFIG_CS_KEY);
		
		$token = $original_key == $app_key ? encrypt::en($this->original_token) : '';
		$this->app_out($token);
	}
	//=========================>private
	
	private function app_out($content) {
		//hook ctr_app_app_out.php
		exit(core::json_encode($content));
	}
	
	private function app_valid() {
		//hook ctr_app_app_valid.php
		$token = core::gpc('token', 'G');
		if($this->original_token != encrypt::de($token)) {
			$this->app_out('JYWAP Access Invalid');
		}
	}
	
	//hook ctr_app_end.php
}
/*{TranslateTagEnd}*/

?>